# coding=utf8


class Preliminary:

    @staticmethod
    def demo_numbers():
        a = 100
        b = 1.123
        c = 1 + 2j
        d = 0j
        print(f"a = {a}")
        print(f"b = {format(b, '.20f')}")
        print(f"c = {c}")
        print(f"d = {d}, type(d) = {type(d)}")

    @staticmethod
    def demo_str():
        a = '123'
        b = "abc"
        c = """a'c""d"""
        d = '123.123'
        print(
            f"a = '{a}', a.isdigit() = {a.isdigit()}\n"
            f"b = \"{b}\", b.upper() = {b.upper()}\n"
            f"c = \"\"\"{c}\"\"\", c.startswith('a') = {c.startswith('a')}\n"
            f"d = '{d}', d.isdigit() = {d.isdigit()}"
        )

        def isfloat(digit_str):
            sp = digit_str.split('.')
            if len(sp) == 2:
                if digit_str[-1] == '.':
                    return sp[0].isdigit()
                else:
                    return all([s.isdigit() for s in sp])
            else:
                return False

        print(f"isfloat(a) = {isfloat(a)}")
        print(f"isfloat(d) = {isfloat(d)}")

    def demo_list(self):
        pass

    def demo_tuple(self):
        pass

    def demo_dict(self):
        pass

    def demo_set(self):
        st1 = {1, 2, 3}
        st2 = {2, 4, 6}
        st3 = {1, 2}
        print(
            " >>> st1 = {1, 2, 3}\n"
            " >>> st2 = {2, 4, 6}\n"
            " >>> st3 = {1, 2}\n"
            " >>> st3 in st2     # 子集包含判断\n"
            f"{st3 in st2}\n"
            " >>> 1 in st1       # 元素属于判断\n"
            f"{1 in st1}\n"
            " >>> st1 & st2      # 交集\n"
            f"{st1 & st2}\n"
            " >>> st1.intersection(st2)      # 交集\n"
            f"{st1.intersection(st2)}\n"
            " >>> st1 | st2      # 并集\n"
            f"{st1 | st2}\n"
            " >>> st1 - st2      # 差集\n"
            f"{st1 - st2}\n"
            " >>> st1 ^ st2      # 对称差集\n"
            f"{st1 ^ st2}\n"
            " >>> st1 - st2      # 差集\n"
            f"{st1 - st2}\n"
        )


def task():
    b = input("请输入一个类型的数据：")
    if b.isdigit():				# 判断是否为整数
        whatis = '这是一个整数'
    elif ''.join(b.rsplit('.')).isdigit():		    # 判断是否为浮点数
        whatis = '这是一个浮点数'
    elif b.startswith('[') and b.endswith(']'):	    # 判断是否为列表
        whatis = '这是一个列表'
    elif b.startswith('(') and b.endswith(')'):	    # 判断是否为元组
        whatis = '这是一个元组'
    elif b.startswith('{') and b.endswith('}'):	    # 判断是否为列表
        isdict = [True if ':' in x else False for x in b]
        if any(isdict):			# 判断是否为字典
            whatis = '这是一个字典'
        else:
            whatis = '这是一个集合'
    else:
        whatis = '这是一个字符串'
    print(whatis)


def expand_train_1():
    mylist = [1, 2, 3, 1, 3, 2]
    print(
        f"mylist = {mylist}\n"
        f"unique elements in mylist: set(mylist) = {set(mylist)}\n"
        f"unique elements number: {len(set(mylist))}"
    )


def expand_train_2():
    x = 10
    p = 'b'
    # 使用字典建立判断流程
    d = {'a': 1, 'b': 3}
    x = d.get(p, -1)
    # 等同于：
    # if p == 'a':
    #     x = 1
    # elif p == 'b':
    #     x = 3
    # else:
    #     x = -1
    print(x)


def expand_train_3():
    d = dict([(s, v) for s, v in zip('abcd', range(1,5))])
    print(d)
    print("遍历字典键：")
    for k in d.keys():
        print(k, end=',')
    print("")
    print("遍历字典值：")
    for v in d.values():
        print(v, end=",")
    print("")
    print("遍历字典键、值：")
    for k, v in d.items():
        print(k, v)
    print("")


if __name__ == '__main__':
    Preliminary().demo_set()
    # Preliminary.string()
    # task()
    # expand_train_1()
    # expand_train_2()
    # expand_train_3()
